package com.whu.MenuHub.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.whu.MenuHub.dao.FoodCommentDao;
import com.whu.MenuHub.domain.FoodComment;
import com.whu.MenuHub.domain.FoodCommentDTO;
import com.whu.MenuHub.domain.LabelNum;
import com.whu.MenuHub.domain.User;
import com.whu.MenuHub.exception.MenuHubException;
import com.whu.MenuHub.service.FoodCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

@Service
public class FoodCommentServiceImpl extends ServiceImpl<FoodCommentDao, FoodComment> implements FoodCommentService {
    @Autowired
    FoodCommentDao judgementDao;

    //获得一条食品评价信息
    @Cacheable(cacheNames = "foodJudgement",key = "#id",condition = "#id!=null")
    @Override
    public FoodComment getJudgementByID(Integer id){
        return judgementDao.selectById(id);
    }

    //新增一条食品评价信息
    @Override
    public FoodComment insertFoodJudgement(FoodComment foodJudgement) throws MenuHubException {
        try{
            judgementDao.insert(foodJudgement);
            return foodJudgement;
        }catch (Exception e){
            throw new MenuHubException(MenuHubException.ErrorType.INSERT_ERROR,"添加食品评价信息失败");
        }
    }

    //修改一条食品评价信息
    @Transactional(rollbackFor = MenuHubException.class)
    @Override
    @CacheEvict(cacheNames = "foodJudgement",key = "#foodJudgement.id")
    public void updateJudgementByID(FoodComment foodJudgement) throws MenuHubException{
        try{
            judgementDao.updateById(foodJudgement);
        }catch (Exception e){
            throw new MenuHubException(MenuHubException.ErrorType.UPDATE_ERROR,"修改食品评价信息失败");
        }
    }

    //删除一条食品评价信息
    @Transactional(rollbackFor = MenuHubException.class)
    @Override
    @CacheEvict(cacheNames = "foodJudgement",key = "#id")
    public void deleteJudgementById(Integer id) throws MenuHubException{
        try{
            judgementDao.deleteById(id);
        }catch (Exception e){
            throw new MenuHubException(MenuHubException.ErrorType.DELETE_ERROR,"删除食品评价信息失败");
        }
    }

    @Override
    public IPage<FoodCommentDTO> getComments(Map<String, Object> condition, Integer pageNum, Integer pageSize){
        IPage page = new Page(pageNum, pageSize);
        QueryWrapper<FoodCommentDTO> qw = new QueryWrapper<>();
        qw.and(wq -> {
            // 拼接sql
            wq.isNotNull("comment")
                    .or()
                    .notLike("foodscore",0);
        });
        qw.eq(condition.containsKey("foodID"), "foodID", condition.get("foodID"));
        qw.eq(condition.containsKey("orderID"), "orderID", condition.get("orderID"));
//        qw.like(condition.containsKey("keyword"), "comment", condition.get("keyword"));
        if(condition.containsKey("keyword")){
            //获取关键词数组
         List<String> keywords = (List<String>) condition.get("keyword");
        //用数组中的词作为关键词
        for(String keyword:keywords){
            System.out.println(keyword);
            qw.like( "comment",keyword);
            }
        }
        if(condition.containsKey("starLevel")){
            String starLevel= condition.get("starLevel").toString();
            if(starLevel.equals("好")){
                qw.ge( "foodscore", 4);
            }else if(starLevel.equals("中")){
                qw.eq( "foodscore", 3);
            }else{
                qw.lt( "foodscore", 3).ge("foodscore",1);
            }
        }
        return judgementDao.findFoodComments(page, qw);
    }
    @Override
    public List<FoodComment> getAllCommentsByUserAndFood(Integer userID, Integer foodID){
        QueryWrapper<FoodComment> qw = new QueryWrapper<>();
        qw.eq( "foodID", foodID);
        qw.eq("userID", userID);
        return judgementDao.selectList(qw);
    }

    //获取到某籍贯对商品的平均评分
    public List<Float> getFoodAveScoreByAddress(String address){
        return judgementDao.getFoodAveScoreByAddress(address);
    }

    //获取到某性别对商品的平均评分
    public List<Float> getFoodAveScoreByGender(String gender){
        return judgementDao.getFoodAveScoreByGender(gender);
    }


    //获取用户某标签对应的订单数量
    public List<LabelNum> getlabelNumByUserOrder(Integer userID){
        return judgementDao.getlabelNumByUserOrder(userID);
    }

    //根据食品ID获取所有的评价信息
    public List<FoodComment> getAllCommentsByFood(Integer foodID){
        QueryWrapper<FoodComment> qw = new QueryWrapper<>();
        qw.eq( "foodID", foodID);
        return judgementDao.selectList(qw);
    }
}
